package com.extrace.loader;

import android.app.Activity;
import android.widget.Toast;

import com.extrace.misc.model.TransPackage;
import com.extrace.misc.model.TransPackageContent;
import com.extrace.net.HttpAsyncTask;
import com.extrace.net.HttpResponseParam.RETURN_STATUS;
import com.extrace.net.IDataAdapter;
import com.extrace.net.JsonUtils;
import com.extrace.ui.main.ExTraceApplication;
import com.google.gson.reflect.TypeToken;

public class TransPackageLoader extends HttpAsyncTask {

    String url;
    IDataAdapter<TransPackage> adapter;
    private Activity context;

    public TransPackageLoader(IDataAdapter<TransPackage> adpt, Activity context) {
        super(context);
        this.context = context;
        adapter = adpt;
        url = ((ExTraceApplication)context.getApplication()).getDomainServiceUrl();
    }

    @Override
    public void onDataReceive(String class_name, String json_data) {
        if(class_name.equals("TransPackage"))
        {
            TransPackage ci = JsonUtils.fromJson(json_data, new TypeToken<TransPackage>(){});
            adapter.setData(ci);
            adapter.notifyDataSetChanged();
        }
        else if(class_name.equals("R_TransPackage"))		//保存完成
        {
            TransPackage ci = JsonUtils.fromJson(json_data, new TypeToken<TransPackage>(){});
            adapter.getData().setID(ci.getID());
            adapter.getData().onSave();
            adapter.notifyDataSetChanged();
            Toast.makeText(context, "包裹信息保存完成!", Toast.LENGTH_SHORT).show();
        }
        else if(class_name.equals("R_TransPackageContent"))
        {
            Toast.makeText(context, "包裹详细信息保存完成！", Toast.LENGTH_SHORT).show();

        }
        else if(class_name.equals("N_TransPackage"))
        {
            TransPackage ci = JsonUtils.fromJson(json_data, new TypeToken<TransPackage>(){});
            adapter.setData(ci);
            adapter.notifyDataSetChanged();
            Toast.makeText(context, "不是该网点的包裹！", Toast.LENGTH_SHORT).show();
        }
        else if(class_name.equals("S_TransPackage"))
        {
            TransPackage ci = JsonUtils.fromJson(json_data, new TypeToken<TransPackage>(){});
            adapter.setData(ci);
            adapter.notifyDataSetChanged();
            Toast.makeText(context, "接受转运任务成功！", Toast.LENGTH_SHORT).show();
        }
        else if(class_name.equals("F_TransPackage"))
        {
            TransPackage ci = JsonUtils.fromJson(json_data, new TypeToken<TransPackage>(){});
            adapter.setData(ci);
            adapter.notifyDataSetChanged();
            Toast.makeText(context, "不是需要转运的包裹！", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onStatusNotify(RETURN_STATUS status, String str_response) {
        // TODO Auto-generated method stub

    }

    public void Load(String id)
    {
        url += "getTransPackage/"+ id + "?_type=json";
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Save(TransPackage tp)
    {
        String jsonObj = JsonUtils.toJson(tp, true);
        url += "saveTransPackage";
        try {
            execute(url, "POST", jsonObj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void New(String id) {
        int uid = ((ExTraceApplication)context.getApplication()).getLoginUser().getUID();
        url += "newTransPackage/id/" + id + "/uid/" + uid + "?_type=json";
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void SaveTPC(TransPackageContent list) {
        //for (TransPackageContent tpc : list) {
            String jsonObj = JsonUtils.toJson(list, true);
            url += "saveTransPackageContent";
            try {
                execute(url, "POST", jsonObj);
            } catch (Exception e) {
                e.printStackTrace();
            }
        //}
    }

    public void SaveHistory(String id, int method) {
        int uid = ((ExTraceApplication)context.getApplication()).getLoginUser().getUID();
        url += "saveTransHistory/id/" + id + "/uid/" + uid + "/method/" + method +"?_type=json";
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void TransPackageByUID(String id) {
        int uid = ((ExTraceApplication)context.getApplication()).getLoginUser().getUID();
        url += "transPackageByUid/pkgId/" + id + "/uid/" + uid +"?_type=json";
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
